<?php

class Boot_suggestion {
	public function __construct() {
		add_action( 'wp_enqueue_scripts', array(&$this, 'enqueue_scripts'), 100 );
		add_shortcode( 'boot_suggestion', array(&$this, 'show_html'));
	}
	
	/**
	 * Adds new stylesheets and scripts
	 * 
	 */
	public function enqueue_scripts()
	{
		wp_enqueue_style( 'dashicons' );
		wp_register_style('jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css');
  		wp_enqueue_style( 'jquery-ui' );   
		wp_enqueue_style( 'bs-main', BS_ASSETS_URL.'css/main.css', array(), '1.0' );
	   
	   
		wp_enqueue_script( 'jquery-ui-slider', array( 'jquery' ) );
		wp_enqueue_script( 'bs-main', BS_ASSETS_URL.'js/main.js', array( 'jquery', 'jquery-ui-slider' ), '1.0', true );
	}

	public function show_html( $atts ) {
		$player_type_terms = get_terms('player_type');
		$foot_type_terms = get_terms('foot_type');
		$surface_terms = get_terms('surface');
		$material_terms = get_terms('material');
		$level_terms = get_terms('level');
		$brand_terms = get_terms('brand');
		
//		$output = '';
		$output = ob_start();
		?>
		<div id="data-input" class="clearfix">
			<div id="dropdown-choices" class="clearfix">
				<div class="dropdown-choice clearfix" id="playertype-choice">
					<span>Spillertype</span>
					<select id="select-playertype">
					<option value="">Vælg spillertype</option>
					<?php
					foreach($player_type_terms as $term=>$value) {
						echo '<option value="' . $value->name . '">' . $value->name . '</option>';
					}
					?>		
					</select>
				</div>
				<div class="dropdown-choice clearfix" id="foottype-choice">
					<span>Fodtype</span>
					<select id="select-foottype">
						<option value="">Vælg fodtype</option>
						<?php
						foreach($foot_type_terms as $term=>$value) {
							echo '<option value="' . $value->name . '">' . $value->name . '</option>';
						}
						?>
					</select>
				</div>
				<div class="dropdown-choice clearfix" id="surface-choice">
					<span>Underlag</span>
					<select id="select-surface">
						<option value="">Vælg underlag</option>
						<?php
						foreach($surface_terms as $term=>$value) {
							echo '<option value="' . $value->name . '">' . $value->name . '</option>';
						}
						?>	
					</select>
				</div>
				<div class="dropdown-choice clearfix" id="level-choice">
					<span>Niveau</span>
					<select id="select-level">
						<option value="">Vælg niveau</option>
						<?php
						$level_terms = get_terms('level');
						foreach($level_terms as $term=>$value) {
							echo '<option value="' . $value->name . '">' . $value->name . '</option>';
						}
						?>
					</select>
				</div>
				<div class="dropdown-choice clearfix" id="brand-choice">
					<span>Mærke</span>
					<select id="select-brand">
						<option value="">Vælg mærke</option>
						<?php
						foreach($brand_terms as $value) {
							echo '<option value="' . $value->name . '">' . $value->name . '</option>';
						}
						?>
					</select>
				</div>
		    </div>
		<div id="material-choices" class="clearfix">
			<span>Materiale</span>
			<div id="material-boxes">
			<?php
			foreach($material_terms as $term=>$value) {
				echo '<div class="material-checkbox"><input type="checkbox" name="material_checkbox" id="' . strtolower($value->name) . '"><label for="' . strtolower($value->name) .'">' . $value->name . '</label></div>';
			}
			?>
			</div>
		</div>
		<div id="price-range" class="clearfix">
			<span>Max pris<input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;"></span>
			<div id="slider-range">
				<div id="slider-range-max"></div>
			</div>
			<p id="min-amount" style="border:0; color:#f6931f;">100 kr.</p>
			<p id="max-amount" style="border:0; color:#f6931f;">1000 kr.</p>
		</div>
		<input type="button" id="boot-button" value="Find støvle">
		</div>
		<div id="results">
			<?php
			$boots = get_posts(array('posts_per_page'=>9999999999, 'post_type'=>'boots'));
			$dir = rtrim(ABSPATH, '/') . "\wp-content\plugins\affiliate-plugin\AffiliatePlugin.php";
			include_once BS_DIR . $dir;
			$af_plugin = new AffiliatePlugin();
			foreach($boots as $boot) {
				$player_type = wp_get_post_terms($boot->ID, 'player_type');
				$foot_type = wp_get_post_terms($boot->ID, 'foot_type');
				$surface = wp_get_post_terms($boot->ID, 'surface');
				$materials = wp_get_post_terms($boot->ID, 'material');
				$material_filtered = array();
				foreach ($materials as $material) {
					$material_filtered[] = $material->name;
				}
				$material_string = implode(", ", $material_filtered);
				$level = wp_get_post_terms($boot->ID, 'level');
				$brand = wp_get_post_terms($boot->ID, 'brand');
				$boot_page_url = get_permalink($boot->ID);
				$price_tables = wp_get_post_terms($boot->ID, 'pricetable');
		    	$cheapest_price = 99999;
		    	$cheapest_price_link = "";

		    	foreach($price_tables as $index=>$value) {
		    		if($af_plugin->getBestTablePrice($price_tables[$index]->term_id)->price < $cheapest_price) {
		    			$cheapest_price = $af_plugin->getBestTablePrice($price_tables[$index]->term_id)->price;
		    			$cheapest_price_link = $af_plugin->getBestTablePriceLink($price_tables[$index]->term_id);
		    		}
		    	}

				echo '<div class="boot hide" style="" data-playertype="' . strtolower($player_type[0]->name) . '" data-foottype="' . strtolower($foot_type[0]->name) . '" data-surface="' . strtolower($surface[0]->name) . '" data-material="' . strtolower($material_string) . '" data-price="' . $cheapest_price . '" data-level="' . strtolower(str_replace("Ø", "oe", $level[0]->name)) . '" data-brand="' . strtolower($brand[0]->name) . '">';
				echo '<span>' . $brand[0]->name . " " . $boot->post_title . '</span>';
				echo '<img src="' . wp_get_attachment_url(get_post_thumbnail_id($boot->ID)) . '">';
				echo '<span class="price-info">' . $cheapest_price . ' kr.</span>';
				echo '<div class="best-price-buttons clearfix">';
				echo '<a class="best-price-btn" href="' . $boot_page_url . '">Se priser</a>';
				echo '<a class="best-price-btn" href="' . $cheapest_price_link . '">Køb nu</a>';
				echo '</div>';
				echo '<ul class="boot-info-list">';
				echo '<li class="level-info">' . $level[0]->name . '</li>';
				echo '<li class="foottype-info">' . $foot_type[0]->name . '</li>';
				echo '<li class="playertype-info">' . $player_type[0]->name . '</li>';
				echo '<li class="material-info">' . $material_string . '</li>';
				echo '<li class="surface-info">' . $surface[0]->name . '</li>';
				//echo '<li class="price-info">' . $cheapest_price . ' kr</li>';
				echo '</ul>';
				echo '</div>';
			}
			echo '<span id="no-boot-match">Der kunne desværre ikke findes nogle støvler, der matcher dine krav. Prøv igen.</span>';
			?>
		</div>
		<?php
		
		$output = ob_get_clean();
		ob_flush();
		
		return $output;
	}
}

new Boot_suggestion(); 